﻿using GFE.Interfaces.Sample.Consts;
using GFE.Interfaces.Sample.Services;
using GFE.SharePoint.ModelFactory;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Data;
using System.Linq;
using System.Text;

namespace GFE.Customers.Sample.Services
{
    [ExportAttribute(typeof(ICustomerService))]
    public class CustomerService : ICustomerService
    {
        public CustomerService() { }

        public IEnumerable<Interfaces.Sample.Models.CustomerModel> GetCustomerList()
        {

            var result = new List<Interfaces.Sample.Models.CustomerModel>();

            using (new SPMonitoredScope("GetCustomerList"))
            {

                //
                // This is for test purpose.. emulating getting data from a SPList or CrossListQueryCache
                // with the command .GetDataTable / .GetSiteData(...) returning a DataTable
                //
                var testdata = new DataTable();
                testdata.Columns.Add(ColumnConsts.CustomerListColumn_Name);
                testdata.Columns.Add(ColumnConsts.CustomerListColumn_Address);
                testdata.BeginLoadData();

                testdata.LoadDataRow(new object[] { "test person 1", "test address 1" }, true);
                testdata.LoadDataRow(new object[] { "test person 2", "test address 2" }, true);
                testdata.LoadDataRow(new object[] { "test person 3", "test address 3" }, true);

                testdata.EndLoadData();

                //
                // After data is fetched.. use the ModelFactory to create the Model instance.. 
                // Use .CreateFromDataRow(DataRow) for data fetched as DataTable or
                // Use .CreateFromListItem for data fetched as SPListItem (Items)
                //

                if (testdata.Rows.Count == 0) return result;

                foreach (DataRow row in testdata.Rows)
                {
                    result.Add(GFEModelFactory<Interfaces.Sample.Models.CustomerModel>.CreateFromDataRow(row));
                }

                return result;

            }
        }
    }
}
